#include<iostream>
#include<typeinfo>
#include<cassert>
#include<unistd.h>
class Any
{
private:
    //抽象父类
    class holder
    {
    public:
        virtual ~holder(){}
        virtual const std::type_info& type() = 0;//获取数据类型
        virtual holder* clone() = 0;//克隆
    };
    template<class T>
    class placeholder:public holder
    {
    public:
        placeholder(const T& val):_val(val){};
        virtual const std::type_info& type()
        {
            return typeid(T);
        }
        virtual holder* clone()
        {
            return new placeholder(_val);
        }
        T _val;
    };
    holder* _content;
public:
    Any& Swap(Any& any)
    {
        std::swap(this->_content,any._content);
        return *this;
    }
    Any():_content(nullptr){}
    template<class T>
    Any(const T& val)
    :_content(new placeholder<T>(val))
    {}
    Any(const Any& any)
    :_content(any._content ? any._content->clone() : nullptr)
    {}

    ~Any(){
        delete _content;
    }
    
    Any& operator=(const Any& any)
    {
        Any(any).Swap(*this);
        return *this;
    }
    template<class T>
    Any& operator=(const T& val)
    {
        Any(val).Swap(*this);
        return *this;
    }
    template<class T>
    T* get()
    {
        assert(_content->type() == typeid(T));
        return &((placeholder<T>*)_content)->_val;
    }
};
class Test
{
public:
    Test(){std::cout<<"构造"<<std::endl;};
    Test(const Test&){std::cout<<"拷贝"<<std::endl;};
    ~Test(){std::cout<<"析构"<<std::endl;};

};
int main()
{
    Test t;
    Any b;
    b = t;
    Any a;
    a = 10;
    int* pa = a.get<int>();
    std::cout<< *pa <<std::endl;
    return 0;
}